package com.handpay.zztong.hp.log;

import java.io.File;

import android.content.Context;
import android.os.Environment;

public class Storage
{
    private static String _internalRootPath;
    private static String _externalRootPath;
    private static Storage _appStorage;
    private static Storage _currentCorporationStorage;
    private static Storage _currentUserStorage;

    private String _internalStorageRoot;
    private String _externalStorageRoot;

    private Storage(String internalStorageRoot, String externalStorageRoot)
    {
        _internalStorageRoot = internalStorageRoot;
        File directory = new File(_internalStorageRoot);
        if (!directory.exists())
        {
            directory.mkdirs();
        }

        _externalStorageRoot = externalStorageRoot;
        directory = new File(_externalStorageRoot);
        if (!directory.exists())
        {
            directory.mkdirs();
        }
    }

    public Storage createSubStorage(String relativePath)
    {
        return new Storage(
                StorageUtility.combinePath(_internalStorageRoot, relativePath),
                StorageUtility.combinePath(_externalStorageRoot, relativePath));
    }

    public String getInternalPath(String relativePath)
    {
        return StorageUtility.combinePath(_internalStorageRoot, relativePath);
    }

    public String getInteranlTempPath()
    {
        return buildInternalPath("Temp/");
    }

    public String getExternalTempPath()
    {
        return buildExternalPath("Temp/");
    }

    public String buildInternalPath(String relativePath)
    {
        String filePath = getInternalPath(relativePath);
        File file = new File(filePath);
        if (!file.exists() || !file.isDirectory())
        {
            file.mkdirs();
        }
        return filePath;
    }

    public String getExternalPath(String relativePath)
    {
        return StorageUtility.combinePath(_externalStorageRoot, relativePath);
    }

    public String buildExternalPath(String relativePath)
    {
        String filePath = getExternalPath(relativePath);
        File file = new File(filePath);
        if (!file.exists() || !file.isDirectory())
        {
            file.mkdirs();
        }
        return filePath;
    }

    public static boolean initialize(Context context, String appNameOnExternalStorage)
    {
        return initialize(context, appNameOnExternalStorage, true);
    }

    public static boolean initialize(Context context, String appNameOnExternalStorage, boolean fallbackToInternal)
    {
        _internalRootPath = context.getApplicationContext().getFilesDir().getAbsolutePath();
        File extStore = Environment.getExternalStorageDirectory();
        if (extStore.exists())
        {
            _externalRootPath = extStore.getAbsolutePath();
        }
        else
        {
            if (fallbackToInternal)
            {
                _externalRootPath = StorageUtility.combinePath(_internalRootPath, "ExtStorage");
            }
            else
            {
                return false;
            }
        }

        _appStorage = new Storage(StorageUtility.combinePath(_internalRootPath, appNameOnExternalStorage), StorageUtility.combinePath(_externalRootPath, appNameOnExternalStorage));
        return false;
    }

    public static Storage getAppStorage()
    {
        return _appStorage;
    }

    public static Storage getCurrentCorporationStorage()
    {
        return _currentCorporationStorage;
    }

    public static void setCurrentCorporationStorage(Storage currentCorporationStorage)
    {
        _currentCorporationStorage = currentCorporationStorage;
    }

    public static Storage getCurrentUserStorage()
    {
        return _currentUserStorage;
    }

    public static void setCurrentUserStorage(Storage currentUserStorage)
    {
        _currentUserStorage = currentUserStorage;
    }

}
